import time

from selenium.webdriver.common.by import By
from selenium.webdriver.support import expected_conditions

from ..base.base_page import BasePage
from ..page.add_member_page import AddMemberPage


class ContactPage(BasePage):
    __MEMBER_LIST_INFO = (By.CSS_SELECTOR, '.member_colRight_memberTable_td:nth-child(2)')
    __ADD_MEMBER_BUTTON = (By.CSS_SELECTOR, '.qui_btn.ww_btn.js_add_member')
    __DELETE_MEMBER_BUTTON = (By.CSS_SELECTOR, '.qui_btn.ww_btn.js_delete')
    __TIPS_INFO = (By.CSS_SELECTOR, '.member_fixedTip_exportLink.js_export_unactived')
    __MEMBER_NAME = (By.CSS_SELECTOR, '.member_colRight_memberTable_td:nth-child(2)')
    __ALL_SELECTED_CHECKBOX = (By.CSS_SELECTOR, '.member_colRight_memberTable_th_Checkbox .ww_checkbox')
    __DELETE_MEMBER_DIALOG = (By.CSS_SELECTOR, '.qui_dialog.ww_dialog.qui_dialog.ww_dialog')
    __DELETE_CONFIRM_BUTTON = (By.CSS_SELECTOR, '.qui_btn.ww_btn.ww_btn_Blue')

    def goto_add_member(self):
        """
        进入添加成员页面
        :return:
        """
        # 2. 点击添加成员按钮
        # WebDriverWait(self.driver, 10).until(
        #     expected_conditions.element_to_be_clickable(
        #         (By.CSS_SELECTOR, '.member_colRight_memberTable_td:nth-child(2)'))
        # )
        self.wait_until(expected_conditions.element_to_be_clickable(
            self.__MEMBER_LIST_INFO))
        self.find_eles(self.__ADD_MEMBER_BUTTON)[1].click()
        return AddMemberPage(self.driver)

    def get_member_info(self):
        """
        返回成员信息
        :return:
        """
        # 等待 提示信息出现
        self.wait_until(expected_conditions.visibility_of_element_located(self.__TIPS_INFO))
        # 获取成员姓名列表
        names = self.find_eles(self.__MEMBER_NAME)
        # 提取成员姓名，放入列表
        name_list = []
        for n in names:
            name_list.append(n.text)
        return name_list

    def delete_all_member(self):
        """
        删除所有成员
        """
        # 等待进入通讯录
        self.wait_until(expected_conditions.visibility_of_element_located(self.__MEMBER_LIST_INFO))
        time.sleep(3)

        # 选中全选按钮并点击
        all_selected_ele = self.wait_until(
            expected_conditions.visibility_of_element_located(self.__ALL_SELECTED_CHECKBOX))
        # if not all_selected_ele.is_selected():
        all_selected_ele.click()

        # 点击删除按钮
        self.find_ele(self.__DELETE_MEMBER_BUTTON).click()

        # 等待确认删除弹窗出现并在此点击删除按钮
        self.wait_until(expected_conditions.visibility_of_element_located(self.__DELETE_MEMBER_DIALOG))
        # self.wait_until(expected_conditions.visibility_of_element_located(self.__DELETE_CONFIRM_BUTTON))
        self.find_ele(self.__DELETE_CONFIRM_BUTTON).click()
